﻿using System.Collections.Generic;

namespace System.Linq.Functional
{
    public static class UnfoldExtensions
    {
        // F# Seq.unfold
        public static IEnumerable<TResult> Unfold<T, TResult>(Func<T, Option<Tuple<TResult, T>>> generator, T start)
        {
            var next = start;

            while (true)
            {
                var res = generator(next);
                if (res.IsNone)
                    yield break;

                yield return res.Value.Item1;

                next = res.Value.Item2;
            }
        }
    }
}
